package com.app.util;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.security.Security;

public class GmBaseUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(GmBaseUtil.class);
    private static final String BASE_RESOURCE_PATH = "src/main/resources/";
    protected static final String UTF_8 = "UTF-8";

    protected GmBaseUtil() {
        throw new IllegalStateException("Utility class");
    }

    protected static String byte2Base64(byte[] bytes) {
        Base64 encoder = new Base64();
        return StringUtils.newStringUtf8(encoder.encode(bytes));
    }

    protected static byte[] base642Byte(String base64Key) {
        Base64 decoder = new Base64();
        byte[] decode = new byte[0];

        try {
            decode = decoder.decode(base64Key.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException var4) {
            LOGGER.error(var4.getMessage(), var4);
        }

        return decode;
    }

    protected static void writeFile(String filePath, byte[] data) throws IOException {
        if (filePath.startsWith("/")) {
            filePath = filePath.substring(1);
        }

        InputStream ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
        if (null == ins) {
            filePath = "src/main/resources/" + filePath;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            boolean isCreate = file.createNewFile();
            if (!isCreate) {
                throw new IOException("create file failed,please check your privilege");
            } else {
                RandomAccessFile raf = null;

                try {
                    raf = new RandomAccessFile(filePath, "rw");
                    raf.write(data);
                } finally {
                    if (raf != null) {
                        try {
                            raf.close();
                        } catch (IOException var12) {
                            LOGGER.error(var12.getMessage(), var12);
                        }
                    }

                }

            }
        } else {
            throw new IOException(filePath + " already exit,if you want to cover,please delete it first");
        }
    }

    public static String loadKeyFromFile(String filePath) throws IOException {
        if (filePath.startsWith("/")) {
            filePath = filePath.substring(1);
        }

        InputStream ins = null;
        BufferedReader bReader = null;

        try {
            ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
            bReader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
            StringBuilder sb = new StringBuilder();

            String s;
            while((s = bReader.readLine()) != null) {
                sb.append(s).append("\n");
            }

            String var5 = sb.toString();
            return var5;
        } finally {
            if (ins != null) {
                try {
                    ins.close();
                } catch (IOException var15) {
                    LOGGER.error(var15.getMessage(), var15);
                }
            }

            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException var14) {
                    LOGGER.error(var14.getMessage(), var14);
                }
            }

        }
    }

    static {
        Security.addProvider(new BouncyCastleProvider());
    }
}
